/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Test;

/**
 *
 * @author percynguyen92
 */
import java.sql.*;
public class TestConnectMySQL 
{

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) throws Exception 
    {
        Connection conn = null;
        try 
        {
            String driver = "com.mysql.jdbc.Driver";
            String url = "jdbc:mysql://localhost:3306/testapplication";
            String username = "root";
            String password = "toor";//mat khau cua ban
            String dbName = "testapplication";//ten csdl ma ban can ket noi
            Class.forName(driver).newInstance();
            conn = DriverManager.getConnection(url + dbName, username, password);
            System.out.println("Kết nối thành công");
        } 
        catch (ClassNotFoundException | InstantiationException | IllegalAccessException | SQLException ex) 
        {
            System.err.println("Không kết nối được với csdl");
            ex.printStackTrace();
        } 
        finally 
        {
            if (conn != null) 
            {
                try 
                {
                    conn.close();
                    System.out.println("Đóng kết nối");
                } catch (Exception ex) 
                {
                    
                }
            }
        }
    }
}
